//  配置
var _config = {
  base: '',
  fetch: '/api/fetch',
  sync: '/api/sync',
  base_url: '/api/base_url', //  查询根域名
  long_url: '/api/long_url'
}

const _urlType = {
  long_url: "投放域名",
  short_url: "短链域名"
}

const _boolStatus = {
  '1': '正常',
  '0': '屏蔽',
  '-1': '检查出错'
}

//  业务逻辑
var _main = {
  data: {

  },
  setTableDate: function (data) {
    var _r = "<tr>\
          <th>分类</th>\
          <th>检查域名</th>\
          <th>阿里备案</th>\
          <th>运营商</th>\
          <th>QQ</th>\
          <th>微信</th>\
          <th>备注说明</th>\
          <th>操 作</th>\
        </tr>";
    for (var key in data) {
      if (key !== "time") {
        var e = data[key]
        _r += '<tr>';
        _r += '<td>' + _urlType[e['type']] + '</td>'
           + '<td><a hre="' + e['url'] + '" target="_blank" >' + e['url'] + '</td>'
           + '<td>' + _boolStatus[e['beian']] + '</td>'
        _r += '<td>' + ( e['isp'].length ? "已拦截" : "正常" ) + '</td>'
        _r += '<td>' + _boolStatus[e['qq']] + '</td>'
           + '<td>' + _boolStatus[e['wx']] + '</td>'
           + '<td>' + e['comment'] + '</td>'
           + '<td><a href="javascript:void(0)" onclick=\'updateDoamin("' + e['type'] + '", "' + e['url'] + '", "DEL")\' >删除</a></td>'
        _r += '</tr>';
      } else {
        document.querySelector("#time").innerHTML = data[key];
      }
    }
    /* data.forEach((e,i) => {

    }) */
    return _r
  },
  fetch: function () {
    var  _params = {type: document.querySelector('#fetch_type').value};
    _utils.get(_config.fetch, _params, (res) => {
      console.log(res)
      document.querySelector("#list_result").innerHTML = this.setTableDate(res);
    })
  },
  sync: function () {
    var  _params = {};
    alert('域名已提交，请提交8分钟后刷新本页面查看');
    _utils.get(_config.sync, _params, (res) => {
      console.log(res);
      this.fetch();
    })
  },
  bindEvents: function () {
    document.querySelector('#sync').addEventListener('click', this.sync.bind(this));
    document.querySelector('#add_domain').addEventListener('click', this.showModal.bind(this));
    document.querySelector('#dm_cancel').addEventListener('click', this.hideModal.bind(this));
    document.querySelector('#dm_add').addEventListener('click', this.updateDoamin.bind(this));
    document.querySelector('#fetch_type').addEventListener('change', this.fetch.bind(this));
  },
  updateDoamin: function (type, val, method) {
    var _t = _main;
    type = type && typeof type === "string" ? type : document.querySelector('#dm_type').value;
    val = val && typeof val === "string" ? val : document.querySelector('#dm_value').value;
    method = (method && method === "DEL") ? "DEL" : "ADD";
    var _params = {};
    _params[type] = val;
    if (method === "DEL") {
      _utils.del(_config[type], _params, (res) => {
        if (res.msg == 'ok') {
          alert('删除成功！');
        }
        console.log(res)
        _t.fetch();
      })
    } else {
      _utils.post(_config[type], _params, (res) => {
        if (res.msg == 'exist') {
          alert(res.notice);
        } else {
          alert('新增成功');
          document.querySelector('#dm_value').value = '';  //  添加完成后删除框子里的内容
          _t.hideModal();
        }
        console.log(res)
        this.fetch();
      })
    }

  },
  showModal: function () {
    document.querySelector("#template_modal").style.display = "block";
    document.querySelector("#mask").style.display = "block";
  },
  hideModal: function () {
    document.querySelector("#template_modal").style.display = "none";
    document.querySelector("#mask").style.display = "none";
  },
  init: function () {
    this.fetch();
    this.bindEvents();
  }
}
//  run
_main.init();

window.updateDoamin = _main.updateDoamin;
